package com.ormbench.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.ormbench.base.dao.AccountDAO;
import com.ormbench.base.dao.BranchDAO;
import com.ormbench.base.dao.DAOFactory;
import com.ormbench.base.dao.HistoryDAO;
import com.ormbench.base.dao.TellerDAO;
import com.ormbench.base.model.Account;
import com.ormbench.base.model.BaseModel;
import com.ormbench.base.model.Branch;
import com.ormbench.base.model.History;
import com.ormbench.base.model.Teller;

public class HibernateDAOFactory implements DAOFactory {
	
	public void configure(String driver, String url, String username, String password) throws Exception {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass(Account.class);
		cfg.addAnnotatedClass(BaseModel.class);
	    cfg.addAnnotatedClass(Branch.class);
	    cfg.addAnnotatedClass(History.class);
	    cfg.addAnnotatedClass(Teller.class);
	    
	    cfg.setProperty("hibernate.connection.driver_class", driver);
	    cfg.setProperty("hibernate.connection.url", url);
	    cfg.setProperty("hibernate.connection.username", username);
	    cfg.setProperty("hibernate.connection.password", password);
	    cfg.setProperty("hibernate.current_session_context_class", "thread");
		
		SessionFactory factory = cfg.buildSessionFactory();
		HibernateUtil.setSessionFactory(factory);
	}

	public void beginTransaction() {
		HibernateUtil.beginTransaction();
	}

	public void commitTransaction() {
		HibernateUtil.commitTransaction();
	}

	public BranchDAO getBranchDAO() {
		return HibernateBranchDAO.getInstance();
	}
	
	public TellerDAO getTellerDAO() {
		return HibernateTellerDAO.getInstance();
	}
	
	public AccountDAO getAccountDAO() {
		return HibernateAccountDAO.getInstance();
	}

	public HistoryDAO getHistoryDAO() {
		return HibernateHistoryDAO.getInstance();
	}
}
